home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
arrays
/
arrays.txt
< prev
next >
Wrap
Text File
|
1997-08-11
|
11KB
|
321 lines
Array Structures in Visual Basic
-----------------------------------------------------------------------
After viewing much source code from differnet sources I've found
very common programming styles that lend themselves to hogging system
resources, and slowing down programs in general. Many beginning
programmers also end up making programming much more difficult on
themselves in the process. We're going to learn a new word today.
ARRAYS
------
Learn it. Live it. Love it. First of all, Arrays make programs run
much faster. Everyone likes fast executing programs. Fast is good.
Slow is bad. If you like your program to run really slow for no reason,
then don't waste your time reading any further. However, if you want
to learn how to make your code execute fast and save yourself lots of
typing too, read on.
Just about any object in Visual Basic can be assigned an Array
Index. The Index is an identifier that allows you to access the
variable based on a set of conditions. The Array value is set at
design time in the (you guessed it) INDEX property. Any object with an
Index property can be placed into an Array.
So *that's* what Index is for...
Now, using Arrays is not designed for every object in a program,
but is extremely helpful when the object in question is duplicated
several times throughout a program. If your object is the only object
of it's type that is accessed the same way, It's not worth assigning
Array values to that object. It won't speed anything up or make
anything easier if this is the case.
Let's look at how much easier it is to control an object with an
Array Index than with variables. We need an example that's at least
somewhat interesting, so we'll use an Image control that will make our
program look a little different than others. We're going to make our
own Pop-Up menu, that looks cooler than the "built in" one. Rather
than have highlighted options, we're going to use "outlined" options
on our Pop-Up menu.
To get the "outline" look, we'll simply change the BorderStyle
property of a Label control from "0; None", to "1; Fixed Single" when
the Mouse Cursor is over the "highlighted" option. We're going to have
10 (ten) different options, simply to illustrate the effects of an
Array over coding each label separartely. The higher the number of
objects (and their properties) being controlled by the Array, the
easier it is on the programmer, and the faster the program will run.
For this example, our Pop-Up menu will work when we Click a Command
Button on Form1.
So, what do we need? Let's see... A start up form (Form1) with a
Command Button control (Command1) and our Pop-Up Menu with 10 Label
controls that will appear when we Click the Command Button on Form1,
and disappear when we select "Cancel". We'll make another form (Form2)
and use it for our actual Pop-Up Menu. We need to add 10 Labels onto
Form2 and make each Option "do something" when we click on it. For
now, we'll make the Command Button Control on our main form display
which Option on our Pop-Up Menu was selected, unless it was "Cancel",
in which case we'll make the Pop-Up Menu disappear.
Here's the code without using an Array:
----------
Private Sub Label1_Click()
Form1.Command1.Caption = "Label1"
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 1
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label2_Click()
Form1.Command1.Caption = "Label2"
End Sub
Private Sub Label2_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 1
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label3_Click()
Form1.Command1.Caption = "Label3"
End Sub
Private Sub Label3_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 1
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label4_Click()
Form1.Command1.Caption = "Label4"
End Sub
Private Sub Label4_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 1
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label5_Click()
Form1.Command1.Caption = "Label5"
End Sub
Private Sub Label5_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 1
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label6_Click()
Form1.Command1.Caption = "Label6"
End Sub
Private Sub Label6_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 1
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label7_Click()
Form1.Command1.Caption = "Label7"
End Sub
Private Sub Label7_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 1
Label8.BorderStyle = 0
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label8_Click()
Form1.Command1.Caption = "Label8"
End Sub
Private Sub Label8_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 1
Label9.BorderStyle = 0
Label10.BorderStyle = 0
End Sub
Private Sub Label9_Click()
Form1.Command1.Caption = "Label9"
End Sub
Private Sub Label9_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle = 1
Label10.BorderStyle = 0
End Sub
Private Sub Label10_Click()
Label10.BorderStyle = 0
Form1.Command1.Caption = "Click Me"
Form2.Visible = False
End Sub
Private Sub Label10_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Label1.BorderStyle = 0
Label2.BorderStyle = 0
Label3.BorderStyle = 0
Label4.BorderStyle = 0
Label5.BorderStyle = 0
Label6.BorderStyle = 0
Label7.BorderStyle = 0
Label8.BorderStyle = 0
Label9.BorderStyle